//
//  AppDelegate.m
//  01_UIView-UIWindow
//
//  Created by dllo on 16/7/14.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "UIView+Frame.h"
@interface AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 给你创建的窗口填颜色 UIColor 系统的颜色类
    _window.backgroundColor = [UIColor cyanColor];
    // 让Window 成为主窗口并显示
    [_window makeKeyAndVisible];
    // 设置Window 的主窗口
    // Xcode7以后 必须设置rootViewcontroller, 否则 崩溃
    _window.rootViewController = [[ViewController alloc] init];
#if 0
    {
    /* 程序的组成结构
     * 1.main.m  主要实现了程序的正常运行
     * 2.APPDelegate 程序的执行者 签订了UIApplicationDelegate(所有的协议方法都是可选的)
     *注意:AppDelegate就是一个普通话的类, 只不过签订协议, 才有了这么多的方法
     * 3.ViewController 视图控制器 主要负责视图管理, 看得见 摸得到.
     * 4.main.sb(视图管理) LaunchScreen.sb(负责启动页) 可视化管理(Xcode7 以前 LaunchScreen.xib)
     * 5.Asset.xcassets 主要用来管理图片素材
     (Xcode 7 以前叫 Images.xcassets)
     * 6. Info.plist (工程配置文件)
     */
    /* 模拟器
     * Command + 1~5 改变模拟器的大小
     * Command + ←/→ 改变模拟器横屏/竖屏切换
     * iOS概述
     */
    /* 1.UIWindow; (程序中只有一个主window)
     * 1> 取消程序的可视化入口,Main.sb
     * 2> 创建主Window对象
     * 3> 创建主Window, 使其大小与屏幕一样
     * UIScreen 系统的屏幕类
     */
  
    
    /*
     * UIWindow继承于UIView
     * UIView (与NSObject相似 是UI阶段的基类, 大部分的UI控件继承于UIView)
     * 学习新类
     * 1> 需要知道新类包含哪些初始化方法
     * 2> 包含哪些行为(方法)和特征(属性)
     * 遇到UIView的子类, 都可用以上的方式创建
     */
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0 , 251, 251)];
    // 设置背景色
    view.backgroundColor = [UIColor redColor];
    // 添加到Window上
    [_window addSubview:view];
    // UIView在屏幕中显示的是矩形
    /* iOS系统坐标系
     * 与数学中的坐标系不同, y轴的正方向是向下的
     * 原点是屏幕的左上角
     
     * Frame Bounds center (UIView的属性)
     * 1 frame  (x, y, width, height) 视图在其父视图上的位置
     * 2 bounds (x, y, width, height) 视图本身的坐标系
     * bounds 的x, y 影响子视图的frame, 改变的是坐标系
     * bounds 的x, y 修改的是本身的坐标系, 影响他的子视图
     * bounds 的width height 修改的是本身的大小, 会以中心点收缩/扩张
     * bounds 的改变不会改变该视图的位置
     * 3 center 改变中心点(影响视图位置)
     */
    // view.frame = CGRectMake(10, 10, 80, 80);
    // bounds 对视图影响的测试
    NSLog(@"改变前%@", NSStringFromCGRect(view.frame));
    NSLog(@"改变前的中心点%@", NSStringFromCGRect(view.frame));
    view.bounds = CGRectMake(0, 0, 100, 100);
    NSLog(@"改变后%@", NSStringFromCGRect(view.frame));
    NSLog(@"改变后的中心点%@", NSStringFromCGRect(view.frame));
    // UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    // view.center = CGPointMake(375 / 2, 667 / 2);
    // subView.backgroundColor = [UIColor yellowColor];
    // [view addSubview:subView];
    /* CGRect CGPoint
     */
    view.bounds = CGRectMake(view.frame.origin.x, view.frame.origin.y, 100, view.frame.size.height);
    // view.frame get方法
    // frame.size 结构体
    // OC 和 C的内容结合, 是一个常量
    // view.frame.size.width = 100
    view.LST_width = 100;
    CGRect rect = view.frame;
    rect.size.width = 100;
    view.frame = rect;
    }
#endif
    // UIView的基本属性
#if 0
    {
    UIView *testview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    // 1.背景颜色
    testview.backgroundColor = [UIColor purpleColor];
    // 2.透明度 alpha 0.0 ~ 1.0 默认1
    testview.alpha = 0.2;
    // 3.tag 标记(编号) 默认为0
    testview.tag = 1001;
    // 根据tag 获取到对应的view __Kindof
    UIView *view = [_window viewWithTag:1001];
    // 4.subViews 子视图
    UIView *arrayView = [_window.subviews objectAtIndex:0];
    NSLog(@"%@", arrayView);
    // 5.superView 父视图
    testview.superview;
    // 6.hidden 隐藏(YES)/显示(NO) 默认值是NO
    testview.hidden = YES;
    // 视图层级
    // addSubview (添加视图)
    // insertSubview:atIndex:(插入视图, 到指定下标)
    // insertSubview:aboveSubview:(插入视图, 到指定视图上面)
    // insertSubView:belowSubView:(插入视图, 到指定视图下面)
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    view1.backgroundColor = [UIColor orangeColor];
    [_window addSubview:view1];
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(10,10, 80, 80)];
    view2.backgroundColor = [UIColor redColor];
    [_window insertSubview:view2 atIndex:1];
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
    view3.backgroundColor = [UIColor blueColor];
    [_window insertSubview:view3 belowSubview:view2];
    // 添加
    [_window addSubview:testview];
    // 移除
    // [_window removeFromSuperview];
    }
#endif
#pragma mark - label
    /* UILabel (用来显示文本的)
     * 如果类自己没有初始化方法, 则使用父类的
     */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 100)];
    label.backgroundColor = [UIColor whiteColor];
    // 1.text
    label.text = @"Im a boy, Im a boy, Im a boy, Im a boy, Im a boy  ";
    // 2.textColor
    label.textColor = [UIColor blackColor];
    // 3.Font
    label.font = [UIFont systemFontOfSize:60];
    // 加粗
    label.font = [UIFont boldSystemFontOfSize:60];
    // 对齐方式 left right center (默认对齐方式left)
    label.textAlignment = 1;
    // numberOfLines (label的行数) 默认为1
    // 不确定行数时 给0;
    label.numberOfLines = 0;
    // lineBreakMode 换行模式, 省略号位置
    label.lineBreakMode = NSLineBreakByTruncatingHead;
    // 阴影颜色 shadowColor
    label.shadowColor = [UIColor blackColor];
    // 阴影位置 shadowOffset
    label.shadowOffset = CGSizeMake(2, 1);
    label.hidden = NO;
    label.layer.borderWidth = 1.5;
    [_window addSubview:label];
#pragma mark - UIButton
    // UIButton
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(20, 200, 300, 100);
    [btn setTitle:@"这是一个Button" forState:UIControlStateNormal];
    [btn setTitle:@"点完了" forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.layer.borderWidth = 1.5;
    btn.backgroundColor = [UIColor purpleColor];
    [_window addSubview:btn];
    // 给Button添加事件
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn removeTarget:self action:@selector(removeBtn:) forControlEvents:UIControlEventTouchUpInside];
#pragma mark - textField
    // UITextField
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 300, 100)];
    // textField.text = @"请输入文字";
    // borderStyle 边缘样式
    // UITextBorderStyleBezel 无
    // UITextBorderStyleLine 边框有边缘线
    // UITextBorderStyleNone
    // UITextBorderStyleRoundedRect 边缘圆角
    
    // placeholder 占位 当text不为空时 placeholder不显示
    textField.placeholder = @"请输入文字";
    // clearsOnBeginEditing 当开始编辑的时候会清空textfield里面内容
    textField.clearsOnBeginEditing = YES;
    textField.borderStyle = UITextBorderStyleBezel;
    textField.textAlignment = 1;
    textField.textColor = [UIColor orangeColor];
    textField.backgroundColor = [UIColor yellowColor];
    //textField.layer.borderWidth = 1.5;
    textField.font = [UIFont systemFontOfSize:20];
    textField.delegate = self;
    [_window addSubview:textField];
    // command + k 显示/收起键盘
      return YES;
}
#warning mark _ 按钮的关联
- (void)clickBtn:(UIButton *)sender  {
    // 结束编辑
    [_window endEditing:YES];
    _window.backgroundColor = [UIColor yellowColor];
}
- (void)removeBtn:(UIButton *)sender {
    
}
#warning mark - textField协议方法
// 开始编辑前, 判断能否进行编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // 可以编辑
    return YES;
    // 不可以编辑 NO
}
// 已经开始编辑时
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
}
// 判断是否结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}
// 已经结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}
// 处理点击return, 需要进行的操作 回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 回收键盘
    [textField endEditing:YES];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
